import axios from "axios-jsonp-pro";
import {Message} from "element-ui";
import Router from "@/router";


axios.create({
    timeout: 2000, 		      // 请求超时时间2秒
    responseType: "json",
    withCredentials: false    // 是否允许带cookie这些
});


axios.interceptors.request.use(config => {
    let token = null
    let storage = window.localStorage.getItem('Token')
    if (storage !== null && storage !== undefined && storage !== '') {
        let time = new Date().getTime()
        let obj = JSON.parse(storage)
        if (time < obj.expire) {
            token = obj.data
        } else {
            window.localStorage.removeItem('Token')
        }
    }
    config.headers.Token = token
    return config
})

axios.interceptors.response.use(res => {
    if (res.data.code != null) {
        Message({
            message: res.data.message,
            type: 'error'
        })
        if (res.data.code === '888') {
            Router.replace('/')
        }
        throw  JSON.stringify(res.data)
    }

    return res
})


const Axios = axios;
export default Axios
